package com.pro.springbootshop.service.impl;

import com.pro.springbootshop.entity.OtherClasses;
import com.pro.springbootshop.entityExtend.OtherClassesReturn;
import com.pro.springbootshop.mapper.OtherClassesMapper;
import com.pro.springbootshop.service.OtherClassesService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class OtherClassesServiceImpl implements OtherClassesService {
    public OtherClassesMapper otherClassesMapper;
    @Autowired
    public void setUserMapper (OtherClassesMapper otherClassesMapper) {
        this.otherClassesMapper = otherClassesMapper;
    }
    @Override
    public List<OtherClasses> findAllByOther() {
        return otherClassesMapper.selectAllByOther();
    }

    @Override
    public OtherClassesReturn getOtherClassesList(Integer page, Integer limit) {
        int pageNo=(page-1)*limit;
        int total=otherClassesMapper.selectAllByOther().size();
        List<OtherClasses> otherClassesList= otherClassesMapper.getOtherClassesList(pageNo,limit);
        return new OtherClassesReturn(otherClassesList,total);
    }

    @Override
    public String saveOtherClasses(int mainClassId, String otherClassName) {
        OtherClasses otherClasses=new OtherClasses();
        otherClasses.setOtherClassName(otherClassName);
        otherClasses.setMainClassId(mainClassId);
        if(otherClassesMapper.selectOtherClass(mainClassId,otherClassName)==null){
            int i=otherClassesMapper.insert(otherClasses);
            if(i>0){
                return "插入成功";
            }else{
                return "插入失败";
            }
        }else{
            return "已存在";
        }
    }

    @Override
    public String updateOtherClasses(OtherClasses otherClasses) {
        if(otherClassesMapper.selectOtherClass(otherClasses.getMainClassId(),otherClasses.getOtherClassName())==null){
            int i=otherClassesMapper.updateById(otherClasses);
            if(i>0){
                return "修改成功";
            }else{
                return "修改失败";
            }
        }else{
            return "该类已存在";
        }
    }

    @Override
    public String deleteOtherClasses(int otherClassId) {
        int i=otherClassesMapper.deleteById(otherClassId);
        if(i>0){
            return "删除成功";
        }else{
            return "删除失败";
        }
    }
}
